<?php

namespace api\modules\v1\models;

use Yii;
use api\common\models\Common;
use api\common\models\Mar;
use yii\web\NotFoundHttpException;
use yii\web\HttpException;
use api\modules\v1\models\News;
use api\modules\v1\models\Msg;
use api\modules\v1\models\User;
use yii\db\Expression;
use yii\web\Request;
use yii\helpers\Url;
use yii\data\Pagination;
class Attention extends Common
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%attention_app}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['aid', 'required','message'=>'关注的用户ID不能为空'],
            ['aid', 'integer'],
        ];
    }

    /**
     * 用户关注列表
     */
    public function getList()
    {
        $uid = Yii::$app->user->identity->id;
        $query = self::find();
        $query->from(self::tableName() . ' as a');
        $query->leftJoin('{{%user_app}} as b', 'a.uid = b.id');
        $query->select('a.*,b.head_img,b.username');
        $query->where('uid=:uid',[':uid' => $uid]);
        $count = $query->count();

        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->defaultPageSize = Yii::$app->params['page_size'];
        $data = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->orderBy('a.atime asc')
            ->asArray()
            ->all();

        $this->serializePagination($pagination);
        $pagination->defaultPageSize = Yii::$app->params['page_size'];

        $ImgBaseUrl = self::getImgBaseUrl();
        foreach ($data as $k => $v) {
            if ($v['head_img'] != '') {
                $data[$k]['head_img'] = $ImgBaseUrl . substr($v['head_img'], 1);
            }
        }
        $_data = [];
        $_data['items'] = $data;
        $_data['_links'] = $this->linksEnvelope;
        $_data['_meta'] = $this->metaEnvelope;
        return $_data;
    }

    /**
     * 消息发送
     * @param $aid 关注的人id
     * @throws \Exception
     */
    public function sendAttentionMsg($aid,$send_uid=''){
        $msg = new Msg();
        $msg->take_uid = $aid;
        $msg->send_uid = $send_uid == '' ? Yii::$app->user->identity->id : $send_uid;
        $msg->message = '您有一个新粉丝！';
        $msg->type = 3;//关注
        $msg->url = Url::to(['attention/list'], false);
        $msg->time = date('Y-m-d H:i:s');
        $msg->isread = 0;
        $msg->insert();
    }

    /*
     * 根据access-token获取用户id
     * @return string
     * @throws NotFoundHttpException
     */
    public function getTokenUserId()
    {
        $request = new Request();
        $accessToken = $request->get('access-token');
        if (empty($accessToken)) $accessToken = $request->getHeaders()->get('access-token');
        if (is_string($accessToken)) {
            $query = new \yii\db\Query();
            if ($data = $query->from('{{%user_app}}')->select('id,username')->where(['access_token' => $accessToken, 'status' => 10])->one()) {
                return $data;
            }
            throw new NotFoundHttpException('access-token错误.');
        }
        return '';
    }
}
